﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace WebFabricator.org
{
    [Serializable]
    public struct Url:IComparable<Url>,ISerializable
    {
        Uri uri;

        public Url(string sourceUri)
        {
            uri = new Uri(sourceUri).Normalize();
        }

        public Url(Uri sourceUri)
        {
            uri = sourceUri.Normalize();
        }

        public Url(SerializationInfo info, StreamingContext context)
        {
            uri = new Uri(info.GetString("uri"));
        }

        public override bool Equals(object comparand)
        {
            if(comparand is Url)
            {
                Url uri2 = (Url)comparand;
                return uri.ToString().Equals(uri2.uri.ToString());
            }
            else
                return false;
        }

        public int CompareTo(Url other)
        {
            return this.GetBigHashCode().CompareTo(other.GetBigHashCode());
        }

        public static implicit operator Url(Uri uri)
        {
            return new Url(uri);
        }

        public static implicit operator String(Url url)
        {
            return url.ToString();
        }

        public override int GetHashCode()
        {
            return uri.GetHashCode();
        }

        public ulong GetBigHashCode()
        {
            return uri.GetBigHashCode();
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("uri", this.uri.ToString());
        }

        public override string ToString()
        {
            return uri.ToString();
        }
    }
}
